local extension = Package:new("sj_heg_kuo")
extension.extensionName = "sj_heg"
local H = require "packages/hegemony/util"
local U = require "packages/utility/utility"

Fk:loadTranslationTable{
  ["sj_heg_kuo"] = "国战SP武将",
  ["SP_heg"] = "国战SP"
}

local wenyang = General(extension, "SP_heg__wenyang", "wei", 4)
Fk:loadTranslationTable{
  ["SP_heg__wenyang"] = "文鸯",
  ["#SP_heg__wenyang"] = "独骑破军",
  ["illustrator:SP_heg__wenyang"] = "鬼画府",
  ["~SP_heg__wenyang"] = "半生功业，而见疑于一家之言，岂能无怨！",
}
local spquedi = fk.CreateViewAsSkill{
  name = "spquedi",
  anim_type = "offensive",
  enabled_at_play = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) < 1
  end,
  card_filter = function()
    return false
  end,
  view_as = function(self, cards)
    local card = Fk:cloneCard("duel")
    card.skillName = self.name
    return card
  end,
  before_use = function(self, player)
    player.room:askForDiscard(player, 1, 1, false, self.name, true, "slash", "请选择一张【杀】", true)
  end
}
Fk:loadTranslationTable{
  ["spquedi"] = "却敌",
  ["#spquedi_defence"] = "却敌",
  [":spquedi"] = "出牌阶段限一次，若你有【杀】，你可以视为使用一张【决斗】。",
  ["$spquedi1"] = "力摧敌阵，如视天光破云！",
  ["$spquedi2"] = "让尔等有命追，无命回！",
}
wenyang:addSkill(spquedi)
local mobileChoujue = fk.CreateTriggerSkill{
  name = "sp__choujue",
  anim_type = "drawcard",
  events = {fk.Deathed},
  frequency = Skill.Limited,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryGame) == 0
  end,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and data.damage and data.damage.from == player
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:drawCards(player, 3, self.name)
    room:recover({
      who = player,
      num = 1,
      recoverBy = player,
      skillName = self.name,
    })
  end,
}
Fk:loadTranslationTable{
  ["sp__choujue"] = "仇决",
  [":sp__choujue"] = "限定技，当你杀死一名角色后，你摸三张牌并回复1点体力。",
  ["$sp__choujue1"] = "血海深仇，便在今日来报！",
  ["$sp__choujue2"] = "取汝之头，以祭先父！",
}
wenyang:addSkill(mobileChoujue)

local dongyun = General(extension, "SP_heg__dongyun", "shu", 3)
local spbingzheng = fk.CreateActiveSkill{
  name = "spbingzheng",
  anim_type = "drawcard",
  min_card_num = 0,
  target_num = 0,
  prompt = function (self, selected_cards, selected_targets)
    if Self:getHandcardNum() % 2 == 0 then
      return "#spbingzheng2:::"..(Self:getHandcardNum() // 2)
    else
      return "#spbingzheng1"
    end
  end,
  can_use = function(self, player)
    if player:getHandcardNum() % 2 == 0 then
      return not player:isKongcheng() and player:getMark("spbingzheng2-phase") == 0 and player:getMark("spbingzheng1-phase") == 0
    else
      return player:getMark("spbingzheng2-phase") == 0 and player:getMark("spbingzheng1-phase") == 0
    end
  end,
  card_filter = function (self, to_select, selected, selected_targets)
    if Self:getHandcardNum() % 2 == 0 then
      return Fk:currentRoom():getCardArea(to_select) == Player.Hand and not Self:prohibitDiscard(Fk:getCardById(to_select))
    else
      return false
    end
  end,
  target_filter = Util.FalseFunc,
  feasible = function (self, selected, selected_cards)
    if Self:getHandcardNum() % 2 == 0 then
      return #selected_cards >= (Self:getHandcardNum() // 2)
    else
      return true
    end
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    if player:getHandcardNum() % 2 == 0 then
      room:setPlayerMark(player, "spbingzheng2-phase", 1)
      room:throwCard(effect.cards, self.name, player, player)
      if not player.dead then
        player:drawCards(3, self.name)
      end
    else
      room:setPlayerMark(player, "spbingzheng1-phase", 1)
      player:drawCards(3, self.name)
      if not player.dead and not player:isKongcheng() then
        room:askForDiscard(player, player:getHandcardNum() // 2, 999, false, self.name, false, ".",
          "#spbingzheng-discard:::"..(player:getHandcardNum() // 2))
      end
    end
  end,
}
dongyun:addSkill(spbingzheng)
dongyun:addSkill("sheyan")
Fk:loadTranslationTable{
  ["SP_heg__dongyun"] = "董允",
  ["#SP_heg__dongyun"] = "骨鲠良相",
  ["spbingzheng"] = "秉正",
  [":spbingzheng"] = "出牌阶段限一次，若你的手牌数为：奇数，你可以摸三张牌，然后弃置至少半数手牌；偶数，你可以弃置至少半数手牌，然后摸三张牌。",
  ["#spbingzheng1"] = "秉正：你可以摸三张牌，然后弃置至少半数手牌",
  ["#spbingzheng2"] = "秉正：你可以弃置至少%arg张手牌，然后摸三张牌",
  ["#spbingzheng-discard"] = "秉正：请弃置至少%arg张手牌",
  ["$spbingzheng1"] = "自古，就是邪不胜正！",
  ["$spbingzheng2"] = "主公面前，岂容小人搬弄是非！",
  ["~SP_heg__dongyun"] = "大汉，要亡于宦官之手了……",
}

local SP_heg__sunhong = General:new(extension, "SP_heg__sunhong", "wu", 3, 3, General.Male)
local spxianbi = fk.CreateTriggerSkill{
  name = "spxianbi",
  anim_type = "offensive",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target.phase == Player.Finish
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = {}
    room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
      local use = e.data[1]
      if use.from ~= player.id and table.find(TargetGroup:getRealTargets(use.tos), function(pid) return pid == player.id end) then
        table.insertIfNeed(targets, use.from)
      end
    end, Player.HistoryTurn)
    if #targets == 0 then return false end
    -- for _, pid in ipairs(targets) do
    --   room:setPlayerMark(room:getPlayerById(pid), "_spxianbi_slash-phase", 1)
    -- end
    local _, ret = room:askForUseViewAsSkill(player, "#spxianbi_vs", "#spxianbi-choose", true, {bypass_distances = true, bypass_times = true, exclusive_targets = targets})
    for _, pid in ipairs(targets) do
      room:setPlayerMark(room:getPlayerById(pid), "_spxianbi_slash-phase", 0)
    end
    if ret then
      self.cost_data = {ret.cards, ret.targets}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards, tos = table.unpack(self.cost_data)
    local card = Fk:cloneCard("slash")
    card:addSubcards(cards)
    card.skillName = "spxianbi"
    room:useCard{
      from = player.id,
      tos = table.map(tos, function(p) return {p} end),
      card = card
    }
    if (card.color == Card.Black or card.color ~= Card.Black) then
      player:drawCards(1, self.name)
    end
  end,
}
local spxianbi_vs = fk.CreateViewAsSkill{
  name = "#spxianbi_vs",
  card_filter = function(self, to_select, selected)
    return #selected == 0
  end,
  view_as = function(self, cards)
    if #cards < 1 then return end
    local card = Fk:cloneCard("slash")
    card:addSubcards(cards)
    card.skillName = "spxianbi"
    return card
  end
}
local spzenrun = fk.CreateTriggerSkill{
  name = "spzenrun",
  anim_type = "offensive",
  frequency = Skill.Compulsory,
  events = {fk.TargetSpecified},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.card and data.card.trueName == "slash"
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(data.to)
    local card = room:askForCard(to, 1, 1, false, self.name, true, ".|.|.|hand|.|.", "#spzenrun_give:" .. player.id)
    if #card > 0 then
      room:obtainCard(player, card, false, fk.ReasonGive, data.to)
    else
      data.disresponsiveList = data.disresponsiveList or {}
      table.insert(data.disresponsiveList, data.to)
    end
  end,
}
Fk:addSkill(spxianbi_vs)
SP_heg__sunhong:addSkill(spxianbi)
SP_heg__sunhong:addSkill(spzenrun)
Fk:loadTranslationTable{
  ["SP_heg__sunhong"] = "孙弘",
  ["#SP_heg__sunhong"] = "谮诉构争",
  ["illustrator:SP_heg__sunhong"] = "匠人绘",
  ["spxianbi"] = "险诐",
  [":spxianbi"] = "一名角色的结束阶段，你可以将一张牌当【杀】对一名本回合对你使用过牌的其他角色使用，此【杀】结算后你摸一张牌。",
  ["spzenrun"] = "谮润",
  [":spzenrun"] = "锁定技，当你使用【杀】指定目标后，其选择一项：1.交给你一张手牌；2.不可响应此【杀】。",
  ["#spxianbi_vs"] = "险诐",
  ["#spxianbi_prohibit"] = "险诐",
  ["#spxianbi-choose"] = "险诐：你可以将一张手牌当【杀】对一名本回合对你使用过牌的其他角色使用",
  ["#spzenrun_give"] = "谮润：你需交给 %src 一张手牌，否则你不可响应此【杀】",
  ["$spxianbi1"] = "宦海如薄冰，求生逐富贵。",
  ["$spxianbi2"] = "吾不欲为鱼肉，故为刀俎。",
  ["$spzenrun1"] = "据图谋不轨，今奉诏索命。",
  ["$spzenrun2"] = "休妄论芍陂之战，当诛之。",
  ["~SP_heg__sunhong"] = "诸葛公何至于此……",
}

local duyu = General(extension, "SP_heg__duyu", "qun", 4)
local spwuku = fk.CreateTriggerSkill{
  name = "spwuku",
  events = {fk.CardUsing},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return target ~= player and player:hasSkill(self) and player:getMark("@spwuku") < 2 and data.card.type == Card.TypeEquip
  end,
  on_use = function(self, event, target, player)
    player.room:addPlayerMark(player, "@spwuku")
  end,
}
local spmiewu = fk.CreateViewAsSkill{
  name = "spmiewu",
  pattern = ".",
  interaction = function()
    local names, all_names = {} , {}
    for _, id in ipairs(Fk:getAllCardIds()) do
      local card = Fk:getCardById(id)
      if (card.type == Card.TypeBasic or (card.type == Card.TypeTrick and card.sub_type ~= Card.SubtypeDelayedTrick and card.trueName ~= "threaten_emperor")) and not card.is_derived and not table.contains(all_names, card.name) then
        table.insert(all_names, card.name)
        local to_use = Fk:cloneCard(card.name)
        if ((Fk.currentResponsePattern == nil and Self:canUse(to_use) and not Self:prohibitUse(to_use)) or
        (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(to_use))) then
          table.insert(names, card.name)
        end
      end
    end
    if #names == 0 then return false end
    return UI.ComboBox { choices = names, all_choices = all_names }
  end,
  card_filter = function(self, to_select, selected)
    return #selected == 0
  end,
  view_as = function(self, cards)
    if #cards ~= 1 or not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card:addSubcards(cards)
    card.skillName = self.name
    return card
  end,
  before_use = function(self, player, use)
    player.room:removePlayerMark(player, "@spwuku")
  end,
  enabled_at_play = function(self, player)
    return player:getMark("@spwuku") > 0 and player:usedSkillTimes(self.name) == 0
  end,
  enabled_at_response = function(self, player, response)
    return player:getMark("@spwuku") > 0 and player:usedSkillTimes(self.name) == 0
  end,
}
local spmiewu_delay = fk.CreateTriggerSkill{
  name = "#spmiewu_delay",
  events = {fk.CardUseFinished, fk.CardRespondFinished},
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return not player.dead and player == target and table.contains(data.card.skillNames, spmiewu.name)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    player:drawCards(1, spmiewu.name)
  end,
}
duyu:addSkill(spwuku)
spmiewu:addRelatedSkill(spmiewu_delay)
duyu:addSkill(spmiewu)
Fk:loadTranslationTable{
  ["SP_heg__duyu"] = "杜预",
  ["#SP_heg__duyu"] = "文成武德",
  ["illustrator:SP_heg__duyu"] = "鬼画府",
  ["spwuku"] = "武库",
  [":spwuku"] = "锁定技，当其他角色使用装备牌时，你获得1个“武库”标记。（“武库”数量至多为2）",
  ["@spwuku"] = "武库",
  ["spmiewu"] = "灭吴",
  ["#spmiewu_delay"] = "灭吴",
  [":spmiewu"] = "每回合限一次，你可以弃置1个“武库”，将一张牌当做任意一张基本牌或普通锦囊牌（【挟天子以令诸侯】除外）使用或打出；若如此做，你摸一张牌。",
  ["$spwuku1"] = "损益万枢，竭世运机。",
  ["$spwuku2"] = "胸藏万卷，充盈如库。",
  ["$spsanchen1"] = "贼计已穷，陈兵吴地，可一鼓而下也。",
  ["$spsanchen2"] = "伐吴此举，十有九利，惟陛下察之。",
  ["$spmiewu1"] = "倾荡之势已成，石城尽在眼下",
  ["$spmiewu2"] = "吾军势如破竹，江东六郡唾手可得。",
  ["~SP_heg__duyu"] = "洛水圆石，遂道向南，吾将以俭自完耳……",
}















return extension